package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.guigu.health.pojo.Menu;
import com.guigu.health.pojo.User;
import com.guigu.health.service.MenuService;
import com.guigu.health.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName:BbackstageManagementController
 * @Description:后台管理系统
 * @Author:longtao
 * @Date:2021/4/9
 * @Version:1.0
 */

@Controller()
@RequestMapping("/backstage")
public class BackstageManagementController {
    /**
     * 调用userService
     */
    @Reference
    private UserService userService;
    /**
     * 调用menuService服务
     */
    @Reference
    private MenuService menuService;

    /**
     * 后台登录
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/backstageLogin",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    public Object BackstageLogin(@RequestBody User user, HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
          return true;
        }else {
            UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPassword());
            try {
                subject.login(token);
                /**
                 * 传用户名
                 */
                User user1 = userService.loginUser(user.getAccount());
                /**
                 * 获取用户名
                 */
                session.setAttribute("username", user1.getUsername());
                /**
                 * 获取到账号用来判断是否注册会员
                 */
                session.setAttribute("account", user1.getAccount());
                /**
                 * 根据id来获取修改用户
                 */
                session.setAttribute("userID",user1.getId());
                /**
                 * 成功跳转到
                 */
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                /**
                 * 失败跳转到登录页面
                 */
               return false;
            }
        }
        }

    /**
     * 跳转到主页面
     * @return
     */
    @RequestMapping("/goPage")
    public String goPage(){
        return "forward:/WEB-INF\\jsp\\dede\\main.html";
        }

    @RequestMapping("/goPage2")
    public String goPage2(){
        return "forward:/WEB-INF\\jsp\\dede\\page.html";
    }

    @RequestMapping("/goPage3")
    public String goPage3(){
        return "forward:/WEB-INF\\jsp\\dede\\checkitem.html";
    }

    /**
     * 跳转到会员档案
     * @return
     */
    @RequestMapping("/goPage4")
    public String goPage4(){
        return "forward:/WEB-INF\\jsp\\dede\\member.html";
    }

    /**
     * 跳转到预约列表
     * @return
     */
    @RequestMapping("/goPage5")
    public String goPage5(){
        return "forward:/WEB-INF\\jsp\\dede\\Order.html";
    }

    /**
     * 预约设置
     * @return
     */
    @RequestMapping("/goPage6")
    public String goPage6(){
        return "forward:/WEB-INF\\jsp\\dede\\OrderSetting.html";
    }
    /**
     * 跳转套餐折线图页面
     * @return
     */
    @RequestMapping("/goPage7")
    public String goPage7(){
        return "forward:/WEB-INF/jsp/dede/setMealPieChart.html";
    }

    /**
     * 跳转到预约设置
     * @return
     */
    @RequestMapping(value = "/goPage8")
    public String goPage8(){
        return "forward:/WEB-INF/jsp/dede/appointmentSettings.html";
    }

    /**
     * 跳转到会员折线图
     * @return
     */
    @RequestMapping(value = "/goPage9")
    public String goPage9(){
        return "forward:/WEB-INF/jsp/dede/memberBorkenLine.html";
    }

    /**
     * 跳转到套餐管理页面
     * @return
     */
    @RequestMapping(value = "/goPage10")
    public String goPage10(){
        return "forward:/WEB-INF/jsp/dede/PackageManageMent.html";
    }

    /**
     * 跳转到检查项页面
     * @return
     */
    @RequestMapping(value = "/goPage11")
    public String goPage11(){
        return "forward:/WEB-INF/jsp/dede/checkItem.html";
    }

    /**
     * 跳转到检查组页面
     * @return
     */
    @RequestMapping(value = "/goPage12")
    public String goPage12(){
        return "forward:/WEB-INF/jsp/dede/checkGroup.html";
    }

    @RequestMapping(value = "/goPage13")
    public String goPage13(){
        return "forward:/WEB-INF/jsp/dede/role.html";
    }

    @RequestMapping(value = "/goPage14")
    public String goPage14(){
        return "forward:/WEB-INF/jsp/dede/UserManage.html";
    }
    /**
     * 显示菜单栏
     * @return
     *
     */
    @ResponseBody
    @RequestMapping(value = "/showMenuBar",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showMenuBar(){
        String account= (String) SecurityUtils.getSubject().getSession().getAttribute("account");
        List<Menu> all = menuService.queryAll(account);
        return all;
    }

    @ResponseBody
    @RequestMapping(value = "/noPermission",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object noPermission(){
        return false;
    }
}
